package com.hhs.storage.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hhs.storage.bean.*;
import com.hhs.storage.service.JxcMenuRoleService;
import com.hhs.storage.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shadow
 * @since 2018-09-29
 */
@RestController
@RequestMapping("do/role")
@Slf4j
public class RoleController {

    @Resource
    RoleService roleService;

    @RequestMapping(value = "getAllPage", method = RequestMethod.GET)
    public Map<String, Object> getAllPage(InputParam inputParam) {
        log.info("查询角色分页信息");
        EntityWrapper ew = new EntityWrapper<JxcRole>();
        if(StringUtils.isNotEmpty(inputParam.getName())){
            ew.like("name_",inputParam.getName());
        }
        Page<JxcRole> result = roleService.selectPage(new Page<JxcRole>(inputParam.getPage(), inputParam.getSize()),ew);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rows", result.getRecords());
        map.put("total",result.getTotal());
        return map;

    }

    @RequestMapping(value = "getAll", method = RequestMethod.GET)
    public Map<String, Object> getAll() {
        log.info("查询全部角色");
        List<JxcRole> result = roleService.selectList(new EntityWrapper<JxcRole>());
        return Result.sucJsonResp(result);

    }

    @RequestMapping(value = "getAll2", method = RequestMethod.GET)
    public Map<String, Object> getAll2() {
        log.info("查询部分角色");
        List<JxcRole> result = roleService.selectList(
                new EntityWrapper<JxcRole>()
                        .like("name_","会员")
        );
        return Result.sucJsonResp(result);

    }

    @RequestMapping("edit")
    public Map<String, Object> edit(JxcRole jxcRole) {
        log.info("添加角色");
        roleService.insertOrUpdate(jxcRole);
        return Result.sucJsonResp(jxcRole);

    }

    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public Map<String, Object> dele(@PathVariable("id") String id) {
        log.info("删除角色");
        roleService.dele(id);
        return Result.sucJsonResp(null);

    }
    @RequestMapping(value = "{roleId}/menus/{menuIds}", method = RequestMethod.PUT)
    public Map<String, Object> putMenus(@PathVariable("roleId") String roleId,@PathVariable("menuIds") String menuIds) {
        log.info("修改该角色的关联菜单");
        if(StringUtils.isEmpty(roleId))
            return Result.failJsonResp("角色不能为空");
        roleService.putMenus(roleId,menuIds);
        return Result.sucJsonResp(null);

    }

    @RequestMapping(value = "getByUserId/{userId}", method = RequestMethod.GET)
    public Map<String, Object> getByUserId(@PathVariable("userId") String userId) {
        log.info("获取用户的所有角色");
        if(StringUtils.isEmpty(userId))
            return Result.failJsonResp("userId不能为空");
        return roleService.getByUserId(userId);

    }
}

